//
//  VistorLoginView.swift
//  Weibo007
//
//  Created by 闫崇 on 15/12/13.
//  Copyright © 2015年 闫崇. All rights reserved.
//

import UIKit
import FFAutoLayout

//访客视图协议   - 协议需要继承自 NSObjectProtocol
@objc protocol VistroLoginViewDelegate : NSObjectProtocol{
    
    func vistroRegistorButtonClick()
    func vistroLoginButtonClick()
    
}




class VistorLoginView: UIView {

    
    //声明代理  代理都要用 weak 属性关键字
    //swift 属性默认都是 strong代理同样也需要使用 weak
    weak var delegate : VistroLoginViewDelegate?
    

    ///  设置视图内容
    ///
    ///  :param: iconName 图标名称
    ///  :param: message  消息文本
    ///  :param: isHome   是否首页
    func setupViewInfo(iconName:String,message:String,isHome:Bool){
        
        messageLabel.text = message
        
        if isHome {
            
            homeIconView.hidden = false
            iconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
            
        }else{
            homeIconView.hidden = true
            iconView.image = UIImage(named: iconName)
            sendSubviewToBack(maskIconView)
        }
        
        
    }
    
    
    //设置转轮动画
    func startAnimation(){
        
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.toValue = 2 * M_PI
        ani.duration = 20
        
        ani.repeatCount = MAXFLOAT
        
        iconView.layer.addAnimation(ani, forKey: nil)
        
    }
    
    //按钮点击事件
    //注册
    func register() {

        //代理监听方法
        delegate?.vistroRegistorButtonClick()
    }
    
    //登录
    func login() {
        
    
        //代理监听方法
        delegate?.vistroLoginButtonClick()
        
    }
    
    
    //视图的默认构造方法
    //创建控件并且制定布局  用 xib & sb 不会调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    // 解档，用 xib & sb 开发会调用
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK - 创建控件
    private func setupUI() {
        //添加控件
        //能够看见控件添加的层次关系
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
//        print(subviews)
        
        
        //自动布局
        //任何一个控件都可以参照另外一个控件准确的设置位置
        iconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: -40))

        //设置房子的约束
        homeIconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: iconView, size: nil)
        
        //messageLabel 的约束
        messageLabel.ff_AlignVertical(ff_AlignType.BottomCenter, referView: iconView, size: CGSize(width: 240, height: 40), offset: CGPoint(x: 0, y: 20))
        
        
        //注册按钮
        registerButton.ff_AlignVertical(ff_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0, y: 20))

        //登录
        loginButton.ff_AlignVertical(ff_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0, y: 20))
        
        //遮罩视图
        maskIconView.ff_Fill(self, insets: UIEdgeInsets(top: 0, left: 0, bottom: 180, right: 0))
        
        backgroundColor = UIColor(white: 0.93, alpha: 1.0)
        
    }
    
    //MARK - 懒加载图片
    //图标
    lazy var iconView : UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return UIImageView(image: image)
        
    }()
    
    //遮罩视图  不要写系统所拥有的maskView
    lazy var maskIconView : UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return UIImageView(image: image)
        
    }()
    
    //房子
    lazy var homeIconView : UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_image_house")
        
        return UIImageView(image: image)
    }()
    
    //文字标签
    lazy var messageLabel : UILabel = {
        //一旦使用了自动布局 frame 就失效了,在添加控件布局时有效
        let label = UILabel(frame:CGRectMake(0 ,100 ,240,40))
        label.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = NSTextAlignment.Center
        label.preferredMaxLayoutWidth = 224
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    
    //注册按钮
    lazy var registerButton : UIButton = {
        
        let btn = UIButton(frame: CGRectMake(0,0,100,40))
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加点击事件
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        
        return btn
    }()
    
    
    //登录按钮
    lazy var loginButton : UIButton = {
        
        let btn = UIButton(frame: CGRectMake(200,0,100,40))
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加点击事件
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return btn
    }()
    
    
    
}
